<template>
	<z-paging ref="paging" v-model="dataList" @query="queryList">
		<view slot="top">
			<fui-search-bar @search="search" @cancel="searchCancel"></fui-search-bar>
			<fui-filter-bar :items="items" @change="change1"></fui-filter-bar>
			<fui-picker :linkage="true" :options="options" :show="show" @change="change" @cancel="cancel"></fui-picker>
		</view>
		<!-- z-paging默认铺满全屏，此时页面所有view都应放在z-paging标签内，否则会被盖住 -->
		<!-- 需要固定在页面顶部的view请通过slot="top"插入，包括自定义的导航栏 -->
		<view class="card" v-for="(item,index) in dataList" @click="msgRead(item.id,index)">
			<view style="margin-bottom: 10rpx;">{{item.title}}<fui-badge v-if="item.isred==0" type="danger" dot
					style="position: absolute;right: 80rpx;"></fui-badge></view>
			<view style="margin-bottom: 10rpx;">{{item.content}}</view>
			<view style="color: #CCCCCC;font-size: 26rpx;">{{item.createtime}}</view>
		</view>
	</z-paging>
</template>

<script>
	export default {
		data() {
			return {
				// v-model绑定的这个变量不要在分页请求结束中自己赋值，直接使用即可
				dataList: [],
				type: 0,
				keyword: '',
				title: '',
				purchase_code: '',
				items: [{
					text: '消息类型',
					value: '',
					type: 'dropdown',
					active: false
				}],
				options: [{
					text: '全部',
					value: '全部'
				},{
					text: '新订单提醒',
					value: '新订单提醒'
				}, {
					text: '订单更新提醒',
					value: '订单更新提醒'
				}, {
					text: '采购单/退货单发货提醒',
					value: '采购单/退货单发货提醒'
				}, {
					text: '对账单发起提醒',
					value: '对账单发起提醒'
				}, {
					text: '对账单变更提醒',
					value: '对账单变更提醒'
				}, {
					text: '对账单结清提醒',
					value: '对账单结清提醒'
				}, {
					text: '临期/逾期提醒',
					value: '临期/逾期提醒'
				}, {
					text: '裁床数量超出提醒',
					value: '裁床数量超出提醒'
				}],
				show: false,
			};
		},
		onLoad(op) {
			if (op.purchase_code) {
				this.purchase_code = op.purchase_code
			}
		},
		methods: {
			change(e) {
				this.show = false
				console.log('items', e)
				let items = this.items
				
				items[0].value = e.value
				items[0].text = e.text;
				this.items = items;
				if(e.value!='全部'){
					this.title = e.value;
				}else{
					this.title ='';
				}
				this.$refs.paging.reload()
				console.log(this.items)
			},
			cancel() {
				this.show = false
			},
			change1() {
				this.show = true;
			},
			search(e) {
				console.log(e.detail.value)
				this.keyword = e.detail.value;
				this.$refs.paging.reload()
			},
			searchCancel() {
				this.keyword = '';
				this.$refs.paging.reload()
			},
			msgRead(id, index) {
				if (this.dataList[index].isred == 0) {
					this.dataList[index].isred = 1;
					this.$api.msgRead({
						id: id,
					}).then(res => {
						uni.showToast({
							title: '操作成功',
							icon: 'none'
						})
						console.log(res)
					}).catch(err => {
						console.log(err)
						uni.showToast({
							title: err,
							icon: 'none'
						})
					})
				}
			},

			// @query所绑定的方法不要自己调用！！需要刷新列表数据时，只需要调用this.$refs.paging.reload()即可
			queryList(pageNo, pageSize) {
				let self = this;
				console.log({
					type: self.type,
					keyword: self.keyword,
					type: self.type,
					keyword: self.keyword,
					title: self.title,
					purchase_code: self.purchase_code,
					pageNo: pageNo
				})
				this.$api.getMsgList({
					type: self.type,
					keyword: self.keyword,
					title: self.title,
					purchase_code: self.purchase_code,
					pageNo: pageNo
				}).then(res => {
					console.log(res)
					this.$refs.paging.complete(res.data);
				}).catch(err => {
					console.log(err)
					uni.showToast({
						title: err,
						icon: 'none'
					})
					this.$refs.paging.complete(false);
				})
			},
		},
	};
</script>
<style>
	.card {
		border-width: 1px;
		border-style: solid;
		border-color: rgba(121, 121, 121, 1);
		border-radius: 8px;
		margin: 20rpx;
		padding: 20rpx;
	}
</style>